<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区-->
    <el-card>
      <el-alert show-icon type="warning" title="注意：只允许为第三级分类设置相关参数" :closable="false"></el-alert>
      <el-row class="cat_opt">
        <el-col>
            <span>选择商品分类：</span>
            <!--选择商品级联框-->
            <el-cascader expand-trigger="hover" :options="cateList" :props="cateProps" v-model="selectedCateKeys" @change="handleChange">
            </el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
     <!--添加动态参数面版-->
     <el-tab-pane label="动态参数" name="many">
         <el-button type="primary" size="mini" name="many" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加参数</el-button>
         <!--动态参数表格-->
         <el-table :data="manyTableData" border stripe>
            <el-table-column type="expand"></el-table-column>
           <el-table-column label="#" type="index"></el-table-column>
           <el-table-column label="参数名称" prop="attr_name"></el-table-column>
           <el-table-column label="操作">
              <template>
                  <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
           </el-table-column>
         </el-table>
     </el-tab-pane>
     <!--添加静态属性面版-->
     <el-tab-pane label="静态属性" name="only">
        <el-button type="primary" size="mini" name="only" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加属性</el-button>
        <!--静态属性表格-->
         <el-table :data="onlyTableData" border stripe>
            <el-table-column type="expand"></el-table-column>
           <el-table-column label="#" type="index"></el-table-column>
           <el-table-column label="参数名称" prop="attr_name"></el-table-column>
           <el-table-column label="操作">
              <template>
                  <el-button type="primary"  icon="el-icon-edit" size="mini">编辑</el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
           </el-table-column>
         </el-table>
     </el-tab-pane>
     </el-tabs>
    </el-card>
    <!--添加参数的对话框-->
    <el-dialog
  :title="'添加' + titleText"
  :visible.sync="addDialogVisible"
  width="50%">
  <el-form :model="addForm" :rules="addFormRules" @close="addDialogClose" ref="addFormRef" label-width="100px">
    <el-form-item :label="titleText" prop="attr_name">
        <el-input v-model="addForm.attr_name"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addParams">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      // 级联选择框的配置对象
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联选择框双向绑定的数组
      selectedCateKeys: [],
      // 被激活的页签名称
      activeName: 'many',
      // 动态参数的设定
      manyTableData: [],
      // 静态属性的设定
      onlyTableData: [],
      // 控制添加对话框的显示与隐藏
      addDialogVisible: false,
      // 添加参数的表单对象数据
      addForm: {
        attr_name: ''
      },
      // 添加表单的验证规则对象
      addFormRules: {
        attr_name: [
          { required: true, message: '请输入参数名称', trigger: 'blue' }
        ]
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取所有商品分类列表
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      this.cateList = res.data
    },
    // 级联选择框变化会触发这个函数
    handleChange() {
      this.getParamsData()
    },
    // 点击页签的处理事件
    handleTabClick() {
      this.getParamsData()
    },
    // 获取参数的列表数据
    async getParamsData() {
      if (this.selectedCateKeys.length !== 3) {
        this.selectedCateKeys = []
        return
      }
      // 根据所选的id和所处的面板发起请求
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`,
        { params: { sel: this.activeName } })
      // 上方get参数要用params指定
      if (res.meta.status !== 200) {
        return this.$message.error('获取参数列表失败')
      }
      if (this.activeName === 'many') {
        this.manyTableData = res.data
      } else {
        this.onlyTableData = res.data
      }
    },
    // 监听添加对话框地关闭事件
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮添加参数
    addParams() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(`categories/${this.cateId}/attributes`, {
          attr_name: this.addForm.attr_name,
          attr_sel: this.activeName
        })
        if (res.meta.status !== 201) {
          return this.$message.error('添加参数失败！')
        }
        this.$message.success('添加参数成功！')
        this.$refs.addFormRef.resetFields()
        this.addDialogVisible = false
        this.getParamsData()
      })
    }
  },
  computed: {
    // 如果按钮要禁用返回true，否则会false
    isBtnDisabled() {
      if (this.selectedCateKeys.length !== 3) {
        return true
      }
      return false
    },
    // 当前选中的三级分类id
    cateId() {
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[2]
      }
      return null
    },
    // 动态计算对话框标题
    titleText() {
      if (this.activeName === 'many') {
        return '动态参数'
      }
      return '静态属性'
    }
  }
}
</script>

<style lang="less" scope>
.cat_opt{
    margin: 15px 0px;
}
</style>
